﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class FrmMttoFinalizacionContrato : Form
    {
        public int IdContrato;
        [Dependency]
        public IContratoBL contratoBL { get; set; }

        [Dependency]
        public IMotivoFinPeriodoBL motFinPeriodoBL { get; set; }

        public FrmMttoFinalizacionContrato()
        {
            InitializeComponent();
            this.BuildUp();

            cbMotivoFinPeriodo.DataSource = motFinPeriodoBL.GetAllFromMotivoFinPeriodo();
        }

        public CONTRATO _currentContrato { get; set; }

        bool isNew;

        public void NuevoFinContrato()
        {
            isNew = true;

            var contrato = contratoBL.GetFromContratoById(IdContrato);

            //cONTRATOBindingSource.DataSource = _currentContrato;
            cONTRATOBindingSource.DataSource = contrato;
        }

        private void FrmMttoFinalizacionContrato_Load(object sender, EventArgs e)
        {

        }

        private void cbMotivoFinPeriodo_Click(object sender, EventArgs e)
        {
            cbMotivoFinPeriodo.DataSource = motFinPeriodoBL.GetAllFromMotivoFinPeriodo();
        }

        private void cbMotivoFinPeriodo_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbMotivoFinPeriodo.SelectedValue != null)
            {
                idMotivoFinPeriodoTextBox.Text = cbMotivoFinPeriodo.ToString();
            }
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            var finContrato = cONTRATOBindingSource.Current as CONTRATO;

            if (finContrato != null)
            {
                finContrato.Observacion = txtNuevaObservacion.Text.ToUpper();
                finContrato.IdContrato = IdContrato;
                finContrato.FechaTermino = DateTime.Now;
                finContrato.Liquidado = false;
                contratoBL.TerminoContrato(finContrato);
                MessageBox.Show("Contrato Terminado", "Registro",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void txtNuevaObservacion_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void txtNuevaObservacion_KeyPress(object sender, KeyPressEventArgs e)
        {
            var coma = Convert.ToString(",");
            var punto = Convert.ToString(".");
            var parentesis = Convert.ToString("()");
            var puntoComa = Convert.ToString(";");
            var slash = Convert.ToString("/");

            string cadena = coma + punto + parentesis + puntoComa + slash + (char)8;

            if (Char.IsLetter(e.KeyChar) ||cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
